package cn.turing.firecontrol.device.alarm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 *
 * 前后端交互的类实现消息的接收推送(自己发送给所有人(不包括自己))
 *
 * @ServerEndpoint(value = "/test/oneToMany") 前端通过此URI 和后端交互，建立连接
 */
@Slf4j
@ServerEndpoint(value = "/test/oneToMany")
@Component
public class WebSocketServer {
//    static Log log=LogFactory.get(WebSocketServer.class);

    private static int onlineCount = 0;

    private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();

    private Session session;

    private String userId="";

    @OnOpen
    public void onOpen(Session session,@PathParam("userId") String userId) throws InterruptedException, IOException {
        this.session = session;
        this.userId=userId;
        webSocketSet.add(this);
        addOnlineCount();

        log.info("有新连接加入：{}，当前在线人数为：{}", session.getId(), onlineCount);
//开始模拟数据
//        for (int i = 0; i < 1000; i++) {
//            Thread.sleep(1000);//没s发一次
//            if(i%10==0){
//                //发送报警数据
//                this.sendMessage(1369+"");
//            }else {
////                否则就发送正常数据
//                this.sendMessage(600+"");
//            }0.
        ////032
//        }
        try {
            sendMessage("连接成功");
        } catch (IOException e) {

            log.error("用户:"+userId+",网络异常!!!!!!");
        }
    }

    @OnClose
    public void onClose() {
        webSocketSet.remove(userId);
        subOnlineCount();
        log.info("有一连接关闭：{}，当前在线人数为：{}", session.getId(), onlineCount);
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("用户消息:"+userId+",报文:"+message);
    }

    @OnError
    public void onError(Session session, Throwable error) {
        log.error("用户错误:"+this.userId+",原因:"+error.getMessage());
        error.printStackTrace();
    }
    /**
     * 实现服务器主动推送
     */
    public void sendMessage(String message) throws IOException {
        this.session.getAsyncRemote().sendText(message);
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }

    public Session getSession() {
        return session;
    }
    public void setSession(Session session) {
        this.session = session;
    }
    public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() {
        return webSocketSet;
    }

    public static void setWebSocketSet(CopyOnWriteArraySet<WebSocketServer> webSocketSet) {
        WebSocketServer.webSocketSet = webSocketSet;
    }




}